/**
Copyright (c) 2008 Arnaud Fabre <arnaud333@gmail.com>
Copyright (c) 2008 Pierre "delroth" Bourdon <root@delroth.is-a-geek.org>

This file is part of Vos Tutos Offline.

Vos Tutos Offline is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Vos Tutos Offline is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Vos Tutos Offline.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "menubar.h"
#include "mainwindow.h"

#define C(x,y) connect(x, SIGNAL(triggered()), parent, SLOT(y()))
#define CC(x) connect(x, SIGNAL(triggered()), parent, SLOT(x()))

VTOMenuBar::VTOMenuBar(VTOMainWindow* parent) :
    QMenuBar(static_cast<QWidget*>(parent))
{
    this->parent = parent;
    tutotreeDock = parent->getTutoTreeDock();
    initActions();
    initMenus();
    setIcons();
    connectTogether();
}

void VTOMenuBar::disabledTutoMenu()
{
    tutoMenu->setEnabled(false);
    bigtutMenu->setEnabled(false);
    minitutMenu->setEnabled(false);
    newChapter->setEnabled(false);
    newMinituto->setEnabled(false);
    newPart->setEnabled(false);
}

void VTOMenuBar::updateMenu(int type)
{
    disabledTutoMenu();
    if(type == VTO::MINITUTORIAL)
    {
	tutoMenu->setEnabled(true);
	minitutMenu->setEnabled(true);
	newPart->setEnabled(true);
    }
    else if(type == VTO::BIGTUTORIAL)
    {
	tutoMenu->setEnabled(true);
	bigtutMenu->setEnabled(true);
	newChapter->setEnabled(true);
    }
    else if(type == VTO::CHAPTER)
    {
	tutoMenu->setEnabled(true);
	bigtutMenu->setEnabled(true);
	newMinituto->setEnabled(true);
    }
}

void VTOMenuBar::initActions()
{
    viewTutotreeAct = new QAction(tr("Tuto tree"), this);
    viewTutotreeAct->setCheckable(true);
    viewTutotreeAct->setChecked(true);

    showAboutBoxAct = new QAction(tr("About VTO ..."), this);

    /* File Menu */
    close = new QAction(tr("&Close Document"), this);
    close->setShortcut(QKeySequence::Close);

    quit = new QAction(tr("&Quit VTO"), this);

    newNewsItem = new QAction(tr("News item"), this);
    newMiniTutorial = new QAction(tr("Mini tutorial"), this);
    newBigTutorial = new QAction(tr("Big tutorial"), this);

    open = new QAction(tr("Open"), this);
    open->setShortcut(QKeySequence::Open);

    save = new QAction(tr("Save"), this);
    save->setShortcut(QKeySequence::Save);

    saveAs = new QAction(tr("Save As"), this);
	exportZcode = new QAction(tr("Export to zcode (.tuto)"), this);
	exportLatex = new QAction(tr("Export to latex"), this);
	exportHtml = new QAction(tr("Export to HTML"), this);
	
	
    /* Document menu */
    up = new QAction(tr("Up"), this);
    down = new QAction(tr("Down"), this);
    remove = new QAction(tr("Delete"),this);
    
    newChapter = new QAction(tr("New Chapter"), this);
    newMinituto = new QAction(tr("New Minituto"), this);
    newPart = new QAction(tr("New Part"), this);
    
    /* Edit Menu */

    undo = new QAction(tr("U&ndo"), this);
    undo->setShortcut(QKeySequence::Undo);
    undo->setEnabled(false);
    
    redo = new QAction(tr("&Redo"), this);
    redo->setShortcut(QKeySequence::Redo);
    redo->setEnabled(false);
    
    cut = new QAction(tr("C&ut"), this);
    cut->setShortcut(QKeySequence::Cut);
    
    copy = new QAction(tr("&Copy"), this);
    copy->setShortcut(QKeySequence::Copy);
    
    paste = new QAction(tr("&Paste"), this);
    paste->setShortcut(QKeySequence::Paste);
    
    selectAll = new QAction(tr("Select &All"), this);
    selectAll->setShortcut(QKeySequence::SelectAll);
   
   latexconfig = new QAction(tr("&LaTeX configuration"), this);
}

void VTOMenuBar::initMenus()
{
    /* File Menu */
    fileMenu = addMenu(tr("&File"));
    
	newMenu = fileMenu->addMenu(tr("&New"));
	newMenu->addAction(newNewsItem);
	newMenu->addAction(newMiniTutorial);
	newMenu->addAction(newBigTutorial);

	docExport = fileMenu->addMenu(tr("&Export"));
	docExport->addAction(exportZcode);
	docExport->addAction(exportLatex);
	docExport->addAction(exportHtml);

    fileMenu->addSeparator();
    //////
    fileMenu->addAction(open);
    fileMenu->addAction(save);
    fileMenu->addAction(saveAs);

    fileMenu->addSeparator();
    /////

    fileMenu->addAction(close);
    fileMenu->addAction(quit);

    /* Document Menu */

    tutoMenu = addMenu(tr("&Document"));
	tutoMenu->addAction(up);
        tutoMenu->addAction(down);
        tutoMenu->addAction(remove);
	tutoMenu->addSeparator();
	
    minitutMenu = tutoMenu->addMenu(tr("&Mini Tuto Menu"));
      minitutMenu->addAction(newPart);
    bigtutMenu = tutoMenu->addMenu(tr("&Big Tuto Menu"));
      bigtutMenu->addAction(newChapter);
      bigtutMenu->addAction(newMinituto);

    disabledTutoMenu();

    /* Edit Menu */
    editMenu = addMenu(tr("&Edit"));
    
      editMenu->addAction(undo);
      editMenu->addAction(redo);
      editMenu->addSeparator();
      editMenu->addAction(cut);
      editMenu->addAction(copy);
      editMenu->addAction(paste);
      editMenu->addSeparator();
      editMenu->addAction(selectAll);
    
    optionMenu = addMenu(tr("&Option"));
    optionMenu->addAction(latexconfig);
    
    /* View Menu */
    viewMenu = addMenu(tr("&View"));
    viewMenu->addAction(viewTutotreeAct);

    /* Help Menu */
    helpMenu = addMenu(tr("&Help"));
    helpMenu->addAction(showAboutBoxAct);
}

void VTOMenuBar::setIcons()
{
    newNewsItem->setIcon(QIcon(":/icons/res/new_news.png"));
    newMiniTutorial->setIcon(QIcon(":/icons/res/new_mt.png"));
    newBigTutorial->setIcon(QIcon(":/icons/res/new_bt.png"));
    
    open->setIcon(QIcon(":/icons/res/open.png"));
    save->setIcon(QIcon(":/icons/res/save.png"));
    saveAs->setIcon(QIcon(":/icons/res/saveas.png"));
 
    up->setIcon(QIcon(":/icons/res/up.png"));
    down->setIcon(QIcon(":/icons/res/down.png"));
    remove->setIcon(QIcon(":/icons/res/delete.png"));
   
    exportZcode->setIcon(QIcon(":/icons/res/compile_zcode.png"));
    exportLatex->setIcon(QIcon(":/icons/res/compile_tex.png"));
    exportHtml->setIcon(QIcon(":/icons/res/compile_html.png"));
    
    quit->setIcon(QIcon(":/icons/res/quit.png"));
    close->setIcon(QIcon(":/icons/res/close.png"));

    newChapter->setIcon(QIcon(":/icons/res/new_chapter.png"));
    newMinituto->setIcon(QIcon(":/icons/res/new_mt.png"));
    newPart->setIcon(QIcon(":/icons/res/new_part.png"));
}

void VTOMenuBar::connectTogether()
{
    /* Dock Widget Menu */
    connect(viewTutotreeAct, SIGNAL(toggled(bool)),
            tutotreeDock, SLOT(setVisible(bool)));
    
    connect(tutotreeDock, SIGNAL(visibilityChanged(bool)),
            viewTutotreeAct, SLOT(setChecked(bool)));

    // File menu
    C(newNewsItem, newNews);
    CC(newMiniTutorial);
    CC(newBigTutorial);
	
    C(close, closeDocument);
    C(quit, close);
    
    CC(open);
    CC(save);
    CC(saveAs);
	
    CC(exportZcode);
	CC(exportLatex);
	
	// Document menu 
  
    C(up, upSelected);
    C(down, downSelected);
    C(remove, removeSelected);

    CC(newChapter);
    CC(newMinituto);
    CC(newPart);
    
    // Option Menu
    
    CC(latexconfig);
    
    // Edit menu

    connect(parent->getNForm()->getEdNcode(), SIGNAL(undoAvailable(bool)),
	    undo, SLOT(setEnabled(bool)));
	    
    connect(parent->getNForm()->getEdNcode(), SIGNAL(redoAvailable(bool)),
	    redo, SLOT(setEnabled(bool)));	    
	    
    connect(undo, SIGNAL(triggered()),
	    parent->getNForm()->getEdNcode(), SLOT(undo()));
          
    connect(redo, SIGNAL(triggered()),
	    parent->getNForm()->getEdNcode(), SLOT(redo()));
	    
    connect(copy, SIGNAL(triggered()),
	    parent->getNForm()->getEdNcode(), SLOT(copy()));
    
    connect(paste, SIGNAL(triggered()),
	    parent->getNForm()->getEdNcode(), SLOT(paste()));
    
    connect(cut, SIGNAL(triggered()),
	    parent->getNForm()->getEdNcode(), SLOT(cut()));	    

    connect(selectAll, SIGNAL(triggered()),
	    parent->getNForm()->getEdNcode(), SLOT(selectAll()));	  
}
